#coding=utf-8
#LightCam 交易数据统计程序
#init by hfh 20150428

#20141209,084829,trade,01,MAXZHITradeCnt,交易系统,股指成交峰值,3444270,,,

import multiprocessing,subprocess,sys
import socket,os,time,re,traceback,json,binascii
import lc_libs,pymongo


def update_tradedata(oper,name,desc,value):
	try:
		LastTime=int(time.time())
		expr={'name':name}
		sortcont=[("name",pymongo.ASCENDING)]
		datas={'name':name,'desc':desc,'value':value}
		opts=db.queryData(oper,expr,sortcont,0)
		res=db.genResult(opts)

		if res==None:
			db.insertDatas(oper,datas)
		elif len(res)==0:
			db.insertDatas(oper,datas)
		else:
			db.updateData(oper,expr,datas)
	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" :\n"+excinfo+"\n")
		logfile.flush()

def query_tradedata(oper,name):
	try:
		expr={'name':re.compile(name)}
		sortcont=[("name",pymongo.ASCENDING)]
		opts=db.queryData(oper,expr,sortcont,0)
		res=db.genResult(opts)
		print res
		if res==None:
			return 0
		elif len(res)==0:
			return 0
		else :
			return 1
	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" :\n"+excinfo+"\n")
		logfile.flush()

def genjson(filename,data):
	jdata={}
	jdata['rows']=data
	file=open(lchome+"/pyweb/static/json/tradedata/"+filename+".json",'w')
	file.write(json.dumps(jdata))
	file.close()

def genjson_multi(folder,data):
	#{'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'ip':ip,'hostname':hostname,'limsg':limsg,'linum':linum,'fainum':fainum,'status':status,'products':products,'version':version,'lintime':lintime,'exittime':exittime}
	tdata={}
	for agent in data:
		dkey=agent['membernumber']
		if tdata.has_key(dkey):
			tdata[dkey].append(agent)
		else:
			tdata[dkey]=[]
			tdata[dkey].append(agent)
	#cmd="rm -f "+lchome+"/pyweb/static/json/tradedata/"+folder+"/*.json"
	cmd="rm -f %s/pyweb/static/json/tradedata/%s/*.json" % (lchome,folder)
	popen=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
	time.sleep(0.1)
	for dkey in tdata.keys():
		jdata={}
		jdata['rows']=tdata[dkey]
		filename="%s/pyweb/static/json/tradedata/%s/%s.json" % (lchome,folder,dkey)
		file=open(filename,'w')
		#file.write(json.dumps(tdata[dkey]))
		file.write(json.dumps(jdata))
		file.close()

#def genjson_seat(data):
#	#{'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'ip':ip,'hostname':hostname,'limsg':limsg,'linum':linum,'fainum':fainum,'status':status,'products':products,'version':version,'lintime':lintime,'exittime':exittime}
#	tdata={}
#	for agent in data:
#		dkey=agent['membernumber']
#		if tdata.has_key(dkey):
#			tdata[dkey].append(agent)
#		else:
#			tdata[dkey]=[]
#			tdata[dkey].append(agent)
#	cmd="rm -f "+lchome+"/pyweb/static/json/tradedata/seatinfo/*.json"
#	popen=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
#	time.sleep(0.1)
#	for dkey in tdata.keys():
#		jdata={}
#		jdata['rows']=tdata[dkey]
#		file=open(lchome+"/pyweb/static/json/tradedata/seatinfo/"+str(dkey)+".json",'w')
#		#file.write(json.dumps(tdata[dkey]))
#		file.write(json.dumps(jdata))
#		file.close()
#
#	
#def genjson_mdkseat(data):
#	#{'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'ip':ip,'hostname':hostname,'limsg':limsg,'linum':linum,'fainum':fainum,'status':status,'products':products,'version':version,'lintime':lintime,'exittime':exittime}
#	tdata={}
#	for agent in data:
#		dkey=agent['membernumber']
#		if tdata.has_key(dkey):
#			tdata[dkey].append(agent)
#		else:
#			tdata[dkey]=[]
#			tdata[dkey].append(agent)
#	cmd="rm -f "+lchome+"/pyweb/static/json/tradedata/mdkseat/*.json"
#	popen=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
#	time.sleep(0.1)
#	for dkey in tdata.keys():
#		jdata={}
#		jdata['rows']=tdata[dkey]
#		file=open(lchome+"/pyweb/static/json/tradedata/mdkseat/"+str(dkey)+".json",'w')
#		#file.write(json.dumps(tdata[dkey]))
#		file.write(json.dumps(jdata))
#		file.close()
#
#def genjson_linestate(data):
#	#{'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'ip':ip,'hostname':hostname,'limsg':limsg,'linum':linum,'fainum':fainum,'status':status,'products':products,'version':version,'lintime':lintime,'exittime':exittime}
#	tdata={}
#	for agent in data:
#		dkey=agent['membernumber']
#		if tdata.has_key(dkey):
#			tdata[dkey].append(agent)
#		else:
#			tdata[dkey]=[]
#			tdata[dkey].append(agent)
#	cmd="rm -f "+lchome+"/pyweb/static/json/tradedata/linestate/*.json"
#	popen=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
#	time.sleep(0.1)
#	for dkey in tdata.keys():
#		jdata={}
#		jdata['rows']=tdata[dkey]
#		file=open(lchome+"/pyweb/static/json/tradedata/linestate/"+str(dkey)+".json",'w')
#		#file.write(json.dumps(tdata[dkey]))
#		file.write(json.dumps(jdata))
#		file.close()
		
def genjson_mdkseat_sum(data):
	
	sdata={}
	tdata={}
	for agent in data:
		dkey=agent['seatnumber']
		if tdata.has_key(dkey):
			tdata[dkey].append(agent)
			sdata[dkey]['kernels'] += 1
			if agent['status'] == 'offline':
				sdata[dkey]['kernels'] -= 1
				sdata[dkey]['offkernels'] += 1
		else:
			tdata[dkey]=[]
			tdata[dkey].append(agent)
			sdata[dkey]={}
			sdata[dkey]['seatnumber']=agent['seatnumber']
			sdata[dkey]['membername']=agent['membername']
			sdata[dkey]['membernumber']=agent['membernumber']
			sdata[dkey]['kernels']= 1
			sdata[dkey]['offkernels']= 0
			if agent['status'] == 'offline':
				sdata[dkey]['kernels']= 0
				sdata[dkey]['offkernels']= 1
			
	cmd="rm -f "+lchome+"/pyweb/static/json/tradedata/mdkseat_sum/*.json"
	popen=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
	time.sleep(0.1)
	for dkey in tdata.keys():
		jdata={}
		jdata['rows']=tdata[dkey]
		file=open(lchome+"/pyweb/static/json/tradedata/mdkseat_sum/"+str(dkey)+".json",'w')
		#file.write(json.dumps(tdata[dkey]))
		file.write(json.dumps(jdata))
		file.close()

	jdata={}
	jdata['rows']=[]
	for dkey in sdata.keys():
		jdata['rows'].append(sdata[dkey])
	file=open(lchome+"/pyweb/static/json/tradedata/memberseat_mdk_sum.json",'w')
	file.write(json.dumps(jdata))
	file.close()

def trendfunc(input):

	currenttimes=time.localtime()
	current_hour=currenttimes[3]
	current_minute=currenttimes[4]
	current_second=currenttimes[5]
	currenttime=current_hour*3600+current_minute*60+current_second
	
	begin_am=9*3600+15*60
	end_am=11*3600+30*60
	begin_pm=13*3600
	end_pm=15*3600+15*60
	totaltime=4*3600+30*60
	usedtime=1
	
	if currenttime<begin_am:
		usedtime=1
	elif currenttime>begin_am and currenttime<end_am:
		usedtime=currenttime-begin_am
	elif currenttime>end_am and currenttime<begin_pm:
		usedtime=2*3600+15*60
	elif currenttime>begin_pm and currenttime<end_pm:
		usedtime=2*3600+15*60+currenttime-begin_pm
	elif currenttime>end_pm:
		usedtime=4*3600+30*60
	
	return int(input)*totaltime/usedtime
	
def genjson_dict(filename,data):
	tdata=[]
	for key,value in data.items():
		if key=='IF':
			value['index']=1
			key='沪深300股指期货'
		elif key=='IH':
			value['index']=2
			key='上证50股指期货'
		elif key=='IC':
			value['index']=3
			key='中证500股指期货'
		elif key=='T':
			value['index']=4
			key='十年期国债期货'
		elif key=='TF':
			value['index']=5
			key='五年期国债期货'
		elif key=='ZHI':
			value['index']=6
			key='股指期货统计'
		elif key=='ZHAI':
			value['index']=7
			key='国债期货统计'
		elif key=='Total':
			value['index']=8
			key='全市场统计'
			value1 ={}
			value1['index']=9
			value1['name']="全市场今日预期"
			value1['OrderFailCnt']=trendfunc(value['OrderFailCnt'])
			value1['OrderCnt']=trendfunc(value['OrderCnt'])
			value1['MaxTradeCnt']=value['MaxTradeCnt']
			value1['TradeCnt']=trendfunc(value['TradeCnt'])
			value1['MaxOrderCnt']=value['MaxOrderCnt']
			tdata.append(value1)
			#20150603,1591000,20150603,1183000,20150603,407000,20150205,3978
			maxvalue=open('/tmp/max.conf','r').read().split(',')
			otd=int(maxvalue[0])
			otv=int(maxvalue[1])
			od=int(maxvalue[2])
			ov=int(maxvalue[3])
			td=int(maxvalue[4])
			tv=int(maxvalue[5])
			cd=int(maxvalue[6])
			cv=int(maxvalue[7])
			Summary=""
			if int(value['OrderCnt'])+int(value['TradeCnt'])>otv:
				Summary="委托成交笔数达%s,超过历史峰值%s(%s)." % (int(value['OrderCnt'])+int(value['TradeCnt']),otv,otd)
				Summary=Summary.decode('utf8').encode('gbk')
				strs=lc_libs.lc_gen_alert('MONDB1','MONDB1','MonitorHistMaxValue','Order_Trade','4','1',Summary)
				lc_libs.lc_alert(strs)
			if int(value['OrderCnt'])>ov:
				Summary="委托笔数达%s,超过历史峰值%s(%s)." % (int(value['OrderCnt']),ov,od)
				Summary=Summary.decode('utf8').encode('gbk')
				strs=lc_libs.lc_gen_alert('MONDB1','MONDB1','MonitorHistMaxValue','Order','4','1',Summary)
				lc_libs.lc_alert(strs)
			if int(value['TradeCnt'])>tv:
				Summary="成交笔数达%s,超过历史峰值%s(%s)." % (int(value['TradeCnt']),tv,td)
				Summary=Summary.decode('utf8').encode('gbk')
				strs=lc_libs.lc_gen_alert('MONDB1','MONDB1','MonitorHistMaxValue','Trade','4','1',Summary)
				lc_libs.lc_alert(strs)
		value['name']=key
		tdata.append(value)
	jdata={}
	jdata['rows']=tdata
	file=open(lchome+"/pyweb/static/json/tradedata/"+filename+".json",'w')
	file.write(json.dumps(jdata))
	file.close()


#通过席位登录IP判断会员接入链路所在机房
def gen_jifang(ipaddr):
	
	result={}
	jifang = ''
	datacenter=''
	datacenter="外高桥"
	devicename=''
	deviceport=''
	portdesc=''
	
	iprec=r"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)"
	if re.search(iprec,ipaddr):
		ips=ipaddr.split('.')
		
		#先判断是否为三所或托管机房地址
		if int(ips[0]) != 10:
			jifang = ''
		elif int(ips[1]) >= 0 and int(ips[1]) <= 15:
			jifang ="大商所"
		elif int(ips[1]) >= 16 and int(ips[1]) <= 31:
			jifang ="郑商所"
		elif int(ips[1]) >= 32 and int(ips[1]) <= 79:
			jifang = "上期所"
		elif int(ips[1]) >= 80 and int(ips[1]) <= 95:
			jifang = "大商所"
		elif int(ips[1]) >= 96 and int(ips[1]) <= 111:
			jifang = "郑商所"
		elif int(ips[1]) == 116:
			if int(ips[2]) >= 129 and int(ips[2]) <= 131:
				jifang = "深证通"
			elif int(ips[2]) == 132:
				jifang = "大连飞创"
			elif int(ips[2]) == 133:
				jifang = "上证通"
			elif int(ips[2]) >= 134 and int(ips[2]) <= 137:
				jifang = "数讯"
			elif int(ips[2]) == 138:
				jifang = "北京飞创"
			elif int(ips[2]) >= 140 and int(ips[2]) <= 143:
				jifang = "移动金桥"
			elif int(ips[2]) >= 144 and int(ips[2]) <= 147:
				jifang = "数讯"
			elif int(ips[2]) >= 160 and int(ips[2]) <= 167:
				jifang = "移动金桥"
			else:
				jifang = ''
		elif int(ips[1]) == 117:
			if int(ips[2]) == 133:
				jifang = "上证通"
			elif int(ips[2]) >= 134 and int(ips[2]) <= 137:
				jifang = "数讯"
			elif int(ips[2]) >= 140 and int(ips[2]) <= 143:
				jifang = "移动金桥"
			elif int(ips[2]) >= 144 and int(ips[2]) <= 147:
				jifang = "数讯"
			else:
				jifang = ''
		elif int(ips[1]) == 118:
			jifang = "上期技术"
		elif int(ips[1]) >= 128 and int(ips[1]) <= 143:
			jifang = "上期所"
			datacenter="张江"
		elif int(ips[1]) >= 160 and int(ips[1]) <= 175:
			jifang = "大商所"
		elif int(ips[1]) >= 176 and int(ips[1]) <= 191:
			jifang = "郑商所"
		elif int(ips[1]) == 210:
			datacenter="张江"
			if int(ips[2]) >= 0 and int(ips[2]) <= 7:
				jifang = "上期技术"
			elif int(ips[2]) == 8:
				jifang = "北京飞创"
			elif int(ips[2]) == 12:
				jifang = "大连飞创"
			else:
				jifang = ''
		elif int(ips[1]) == 213:
			datacenter="张江"
			if int(ips[2]) >= 0 and int(ips[2]) <= 7:
				jifang = "上期技术"
			elif int(ips[2]) == 8:
				jifang = "上证通"
			else:
				jifang = ''
		elif int(ips[1]) >= 240 and int(ips[1]) <= 255:
			jifang = "保证金"
		else:
			jifang = ''
			
		#对非三所和托管机房地址进行判断
		if jifang=='':
			if re.match("^10.113|^10.114|^10.115|^10.116|^10.117",ipaddr):
				jifang="直连"
				datacenter="外高桥"
			elif re.match("^10.209|^10.211|^10.212|^10.213|^10.214",ipaddr):
				jifang="直连"
				datacenter="张江"
			else:
				jifang="其他"
				datacenter="其他"
		
		#查找localiplist表确认接入路由器和接口信息
		tmp_ip=ipaddr.split('.')
		ipkey="%s.%s.%s" % (tmp_ip[0],tmp_ip[1],tmp_ip[2])
		jfkey="%s-%s" % (jifang.decode('utf8'),datacenter.decode('utf8'))
		if ipkey in localiplist.keys():
			devicename=localiplist[ipkey][0]
			deviceport=localiplist[ipkey][1]
			portdesc=localiplist[ipkey][2]
		elif jfkey in jifanglist.keys():
			devicename=jifanglist[jfkey][0]
			deviceport=jifanglist[jfkey][1]
			portdesc=jifanglist[jfkey][2]
			interfacekey=devicename+"-"+deviceport
			if interfacestatus.has_key(interfacekey):
				if interfacestatus[interfacekey].lower=='down':
					thiskey=jfkey+'bk'
					devicename=jifanglist[thiskey][0]
					deviceport=jifanglist[thiskey][1]
					portdesc=jifanglist[thiskey][2]
	else:
		jifang='其他'
	
	result={
		'jifang':jifang,
		'datacenter':datacenter,
		'devicename':devicename,
		'deviceport':deviceport,
		'portdesc':portdesc
		}
	
	return result
	
	
def genjson_devicesum(filename,data):
	jdata={}
	jdata['rows']=[]
	for key,value in data.items():
		#print "---%s---%s" % (key,value)
		jdata['rows'].append(value)
	file=open(lchome+"/pyweb/static/json/network/"+filename+".json",'w')
	file.write(json.dumps(jdata))
	file.close()



if __name__ == "__main__":
	
	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()

	agent_hostname=lc_libs.lc_gethostname()
	Node=agent_hostname
	
	#初始化数据库
	
	db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

	db.connect()
	Conn=db.getConn()
	if Conn==None:
		print "Can not Connect to the MondbDB"
		exit(0)

	events_dbname='lc_events'
	events_tablename='lc_events'
	
	history_dbname='lc_history'
	history_tablename="lc_onlineuser"
	
	events_db=db.useDatabase(events_dbname)
	events_table=db.UseCollection(events_db,events_tablename)
	
	history_db=db.useDatabase(history_dbname)
	history_table=db.UseCollection(history_db,history_tablename)

	dbname='lc_tradedata'
	tradedatadb=db.useDatabase(dbname)
	
	#交易
	tablename='tradesum'
	tradedatadb.drop_collection(tablename)
	tradesumtable=db.UseCollection(tradedatadb,tablename)

	#会员
	tablename='memberseat'
	tradedatadb.drop_collection(tablename)
	memberseattable=db.UseCollection(tradedatadb,tablename)
	
	#会员席位
	tablename='seatinfo'
	tradedatadb.drop_collection(tablename)
	seatinfotable=db.UseCollection(tradedatadb,tablename)
	
	#行情商席位
	tablename='mdkseat'
	tradedatadb.drop_collection(tablename)
	mdkseattable=db.UseCollection(tradedatadb,tablename)

	seatdetail_crc=0
	clientsdata={}
	
	wan_crc=0
	wandata={}
	localiplist={}
	jifanglist={}
	interfacestatus={}
	
	#定义告警相关次数
	alertflag={
		'offlinenumber_mdk_5':0,
		'offlinenumber_mdk_5_r':0,
		'offlinenumber_mdk':0,
		'offlinenumber_mdk_r':0,
		'offlinenumber_mdk_5_over':0,
		'offlinenumber_mdk_5_over_r':0,
		'mdkseat_checknumber':0,
		'mdkseat_checknumber_r':0,
		'mdkseat_checknumber_over':0,
		'mdkseat_checknumber_over_r':0
	}
	
	while True:
		try:
			
			#clientsfile
			#席位号,昨日客户数,逻辑主席标识(1:是,2:非),指定主席标识(1:是,2:非)
			#030303,14,1,0
			clientsfile=open("/tmp/seatdetail.csv","rb").read()
			filecrc=binascii.crc32(clientsfile)
			if seatdetail_crc != filecrc:
				seatdetail_crc=filecrc
				clientsdata={}
				for lines in clientsfile.split('\n'):
					line=lines.split(',')
					if len(line)>3:
						clientsdata[line[0]]=[line[1],line[2],line[3]]
			#print clientsdata
			
			#会员链路wan表
			#对端名称	会员号	会员类型	运营商	链路类型	带宽	数据中心	接入设备	互联地址本端	互连地址对端	局域网段	总头	接口	专线编号	申请单位
			#上期所北京灾备	(null)	三所	电信	ATM	10M	外高桥	W_JR_JR_R01	10.128.201.10	10.128.201.9	(null)	ATM1/0/0	ATM1/0/0.100		对端
			#中信建投	0115	交易结算会员	电信	SDH	2M	外高桥	W_JR_JR_R04	10.112.0.42	10.112.0.41	10.113.10.0/24	Serial1/1/0	Serial1/1/0.1/2/3/3:0	北京上海30N3999NP	交易所

			wanfile=open(lchome+'/conf/lc_wan.conf',"rb").read()
			filecrc=binascii.crc32(wanfile)
			if wan_crc != filecrc:
				wan_crc=filecrc
				wandata={}
				localiplist={}
				jifanglist={}
				for lines in wanfile.split('\n'):
					if lines=='':
						continue
					if lines[0]=='#':
						continue
					line=lines.split('\t')
					if len(line)<4:
						continue
						
					#局域网段	接入路由器	接入总头	接入端口
					#10.113.109.0/24	W_JR_JR_R03	Serial1/1/0	Serial2/0/0.1/1/3/3:0
					iprec=r"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)"
					if re.search(iprec,line[10]):
						ipkey=line[10].replace('.0/24','')
						localiplist[ipkey]=[line[7],line[11],line[12]]
					else:
						jfkey=line[0].decode('utf8')+'-'+line[6].decode('utf8')
						jifanglist[jfkey]=[line[7],line[11],line[12]]
					#print localiplist.keys()
			
			
			#重要端口判断表
			jfile=open(lchome+'/pyweb/static/json/network/interface.json','rb')
			interfacestatus=json.loads(jfile.read())
			
			#交易统计
			TotalMemDb=''
			TotalSeat=''
			TotalOrderCalCnt=''
			TotalOrderCnt=''
			TotalTradeCnt=''
			TotalOrderFailCnt=''
#			for lines in open('/tmp/tradesize.txt'):
#				line=lines.split(' ')
#				if 'OrderSize' in line:
#					TotalOrderCnt=line[7]
#				elif 'TradeSize' in line:
#					TotalTradeCnt=line[7]
					
			#cat status.txt|grep tkernel1|awk -F, '{print "撤单"$40"错单"$43"内存"$48"委托"$49"成交"$57"席位"$67}'
			#comd='''ssh ywn_monitor1 "cat /monitor/bin/app/TradeMon/status.txt|grep tkernel1"'''
			#comd="cat /tmp/status.txt|grep tkernel1"
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read().split('\n')[0].strip('\n').split(',')
			#print line
			thisfile=open("/tmp/status.txt","rb")
			lines=thisfile.readlines()
			thisfile.close()
			
			line=[]
			for item in lines:
				if "tkernel1" in item:
					line=item.strip('\r\n').split(',')
			
			if len(line)>66:
				TotalSeat=line[66]
				TotalOrderCnt=line[48]
				TotalTradeCnt=line[56]
				TotalOrderFailCnt=line[42]
				TotalOrderCalCnt=line[39]
				TotalMemDb=line[47]
			
				strs="在线席位：%s，委托笔数：%s，成交笔数：%s，错误订单：%s，撤销订单：%s，tkernel内存数据库使用率：%s" % (TotalSeat,TotalOrderCnt,TotalTradeCnt,TotalOrderFailCnt,TotalOrderCalCnt,TotalMemDb)
				#print strs
				file=open(lchome+"/pyweb/static/json/tradedata/total.txt",'w')
				file.write(strs)
				file.close()
				
				
			#天量短信
			#comd="cp /tmp/newrecord.txt "+ lchome+"/pyweb/static/json/tradedata/"
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			cachestr=open('/tmp/newrecord.txt').read()
			try:
				cachestr=cachestr.decode('gbk').encode('utf-8')
			except:
				pass
			fw=open(lchome+"/pyweb/static/json/tradedata/newrecord.txt","w")
			fw.write(cachestr)
			fw.close()
			
			
			
			#交易数据
			#comd='''ssh ywn_monitor1 "ssh config1 "cat /tmp/TFCount.log""'''
			#comd="cat /tmp/TFCount.log"
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read().split('\n')
			
			thisfile=open("/tmp/TFCount.log","rb")
			line=thisfile.readlines()
			thisfile.close()
			
			tradedata={}
			tradesumdata={}
			trademaxdata={}

			ZHIOrderCnt=''
			ZHITradeCnt=''
			ZHIOrderFailCnt=''
			ZHAIOrderCnt=''
			ZHAITradeCnt=''
			ZHAIOrderFailCnt=''
			
			for res in line:
				rest=res.split(',')
				if len(rest)<7:
					continue
				try:
					name=rest[4]
					desc=rest[6]
					value=int(rest[7])
					#update_tradedata(tradesumtable,name,desc,value)
					if re.search("[0-9]{4}OrderCnt",name):
						dname=name.replace('OrderCnt','')
						dtype='OrderCnt'
						if tradedata.has_key(dname):
							tradedata[dname][dtype]=value
						else:
							tradedata[dname]={}
							tradedata[dname][dtype]=value
					elif re.search("[0-9]{4}TradeCnt",name):
						dname=name.replace('TradeCnt','')
						dtype='TradeCnt'
						if tradedata.has_key(dname):
							tradedata[dname][dtype]=value
						else:
							tradedata[dname]={}
							tradedata[dname][dtype]=value
					elif re.search("[0-9]{4}OrderFailCnt",name):
						dname=name.replace('OrderFailCnt','')
						dtype='OrderFailCnt'
						if tradedata.has_key(dname):
							tradedata[dname][dtype]=value
						else:
							tradedata[dname]={}
							tradedata[dname][dtype]=value
					elif re.search("[A-Za-z]+[0-9]{0}OrderCnt",name):
						if 'Whole' in name:
							continue
						dname=name.replace('OrderCnt','')
						dtype='OrderCnt'
						if re.search("MAX",dname):
							dname=dname.replace('MAX','')
							dtype='MaxOrderCnt'
						if tradesumdata.has_key(dname):
							tradesumdata[dname][dtype]=value
						else:
							tradesumdata[dname]={}
							tradesumdata[dname][dtype]=value
					elif re.search("[A-Za-z]+[0-9]{0}TradeCnt",name):
						dname=name.replace('TradeCnt','')
						dtype='TradeCnt'
						if re.search("MAX",dname):
							dname=dname.replace('MAX','')
							dtype='MaxTradeCnt'
						if tradesumdata.has_key(dname):
							tradesumdata[dname][dtype]=value
						else:
							tradesumdata[dname]={}
							tradesumdata[dname][dtype]=value
					elif re.search("[A-Za-z]+[0-9]{0}OrderFailCnt",name):
						dname=name.replace('OrderFailCnt','')
						dtype='OrderFailCnt'
						if re.search("MAX",dname):
							dname=dname.replace('MAX','')
							dtype='MaxOrderFailCnt'
						if tradesumdata.has_key(dname):
							tradesumdata[dname][dtype]=value
						else:
							tradesumdata[dname]={}
							tradesumdata[dname][dtype]=value
				except Exception:
					#print traceback.format_exc()
					continue
			#print tradedata
			#print tradesumdata
			
			genjson_dict('tradedata',tradedata)
			genjson_dict('tradesumdata',tradesumdata)
			
			
			
			
			#会员统计 & 行情商统计
			#活动席位数,离线席位数,总席位数,活动主席位数,离线主席位数,总主席位数,会员名,会员号,会员类型,所在地,会员状态(1红2黄3蓝4绿)
			#"17","6","23","1","1","2","国泰君安","0001","全面结算会员","上海","4"
			#comd='''ssh ywn_monitor1 "cat /monitor/bin/app/TradeMon/memberseat.csv" |grep -E "交易结算会员|全面结算会员|交易会员"|grep -v "总计"|grep -v "统计"'''
			#comd='''cat /tmp/memberseat.csv|grep -E "交易结算会员|全面结算会员|交易会员"|grep -v "总计"|grep -v "统计"'''
			#comd=comd.decode('utf8').encode('gbk')
			
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read().decode('gbk').encode('utf8')
			#line=line.split('\n')
			#print line
			
			thisfile=open("/tmp/memberseat.csv","rb")
			line=thisfile.readlines()
			thisfile.close()
			
			memberseat=[]
			onlinenumber=0
			offlinenumber=0
			
			memberseat_mdk=[]
			onlinenumber_mdk=0
			offlinenumber_mdk=0
			
			#五档行情商离线数
			offlinenumber_mdk_5=0
			
			for res in line:
				res=res.decode('gbk').encode('utf8')
				if "总计" in res or "统计" in res :
					continue
				elif "交易结算会员" in res or "全面结算会员" in res or "交易会员" in res:
					res=res.replace('"','')
					rest=res.split(',')
					#print res
					if len(rest)<7:
						continue
					try:
						membername=rest[6]
						membernumber=rest[7]
						membertype=rest[8]
						local=rest[9]
						status=rest[10]
						seatonline=int(rest[0])
						seatoffline=int(rest[1])
						keyseatonline=int(rest[3])
						keyseatoffline=int(rest[4])
						if int(keyseatonline)>0:
							onlinenumber=onlinenumber+1
						elif int(keyseatonline)==0:
							offlinenumber=offlinenumber+1
						memberseat.append({'membername':membername,'membernumber':membernumber,'membertype':membertype,'local':local,'status':status,'seatonline':seatonline,'seatoffline':seatoffline,'keyseatonline':keyseatonline,'keyseatoffline':keyseatoffline})
					except IndexError:
						continue
				elif "行情商" in res:
					res=res.replace('"','')
					rest=res.split(',')
					#print res
					if len(rest)<7:
						continue
					try:
						membername=rest[6]
						membernumber=rest[7]
						membertype=rest[8]
						local=rest[9]
						status='offline'
						seatonline=int(rest[0])
						seatoffline=int(rest[1])
						if int(seatonline)>0:
							onlinenumber_mdk=onlinenumber_mdk+1
							status='online'
						#elif int(seatoffline)==0:
						else:
							offlinenumber_mdk=offlinenumber_mdk+1
							status='offline'
							if membernumber in ['3000','3001','3002','3003','3004','3005','3012','3019','3020','3027','3030']:
								offlinenumber_mdk_5+=1
						memberseat_mdk.append({'membername':membername,'membernumber':membernumber,'membertype':membertype,'local':local,'status':status,'seatonline':seatonline,'seatoffline':seatoffline,'status':status})
					except IndexError:
						continue
				else:
					continue
			
			#print memberseat
			genjson('memberseat',memberseat)
			membernumbers=float(offlinenumber)+float(onlinenumber)
			if membernumbers==0.0:
				membernumbers=1.0
			strs="活动会员数：%s，离线会员数：%s，离线会员占比：%.2f " % (onlinenumber,offlinenumber,100*float(offlinenumber)/membernumbers)
			file=open(lchome+"/pyweb/static/json/tradedata/membertotal.txt",'w')
			file.write(strs+"%")
			file.close()
			
			#定义会员总数
			members_numbers=onlinenumber+offlinenumber
			
			#print memberseat_mdk
			genjson('memberseat_mdk',memberseat_mdk)
			
			membernumbers_mdk=float(offlinenumber_mdk)+float(onlinenumber_mdk)
			if membernumbers_mdk==0.0:
				membernumbers_mdk=1.0
			strs="活动行情商数：%s，离线行情商数：%s，离线行情商占比：%.2f " % (onlinenumber_mdk,offlinenumber_mdk,100*float(offlinenumber_mdk)/membernumbers_mdk)
			file=open(lchome+"/pyweb/static/json/tradedata/mdktotal.txt",'w')
			file.write(strs+"%")
			file.close()

			#行情商相关告警
			mdk_schedule='0-4,9:15-11:30|0-4,12:45-15:00'
			time_state = lc_libs.lc_schedule(mdk_schedule)
			if time_state == True:
				#五档行情商席位掉线告警:
				if offlinenumber_mdk_5>0:
					if alertflag['offlinenumber_mdk_5']>=2:
						alertflag['offlinenumber_mdk_5']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuote5OfflineErr'
						Type=1
						Severity=4
						Summary="有五档行情商席位掉线"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk_5'] += 1
				else:
					if alertflag['offlinenumber_mdk_5_r']>=2:
						alertflag['offlinenumber_mdk_5_r']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuote5OfflineErr'
						Type=2
						Severity=2
						Summary="五档行情商席位掉线事件恢复"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk_5_r'] += 1

				#四家以上行情商席位掉线告警:
				if offlinenumber_mdk>=4:
					if alertflag['offlinenumber_mdk']>=2:
						alertflag['offlinenumber_mdk']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuoteOfflineErr'
						Type=1
						Severity=4
						Summary="有4家以上行情商席位掉线"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk'] +=1
				else:
					if alertflag['offlinenumber_mdk_r']>=2:
						alertflag['offlinenumber_mdk_r']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuoteOfflineErr'
						Type=2
						Severity=2
						Summary="4家以上行情商席位掉线事件恢复"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk_r'] +=1

				#超过6家五档行情商席位掉线告警:
				if offlinenumber_mdk_5>6:
					if alertflag['offlinenumber_mdk_5_over']>=2:
						alertflag['offlinenumber_mdk_5_over']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuote5OfflineCri'
						Type=1
						Severity=5
						Summary="有超过6家五档行情商席位掉线"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk_5_over'] += 1
				else:
					if alertflag['offlinenumber_mdk_5_over_r']>=2:
						alertflag['offlinenumber_mdk_5_over_r']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberQuote5OfflineCri'
						Type=2
						Severity=2
						Summary="超过6家五档行情商席位掉线事件恢复"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['offlinenumber_mdk_5_over_r'] += 1


			#会员逻辑席位统计 & 会员指定席位统计
			#SEATINFO,010907,0109,银河期货,1,10.118.136.113,front3,正确,1,0,1,online,SFTP V1.0,Lnx64 TraderAPI v1.5 L300,
			#2014070107:47:24, ,2014070115:14:55,5,0,8,2856,21611,3,13,2014070115:22:09,10,2,12,772631,3,18,9011026, , , , , , , , , , , , , , , , , , ,
			#comd='''ssh ywn_monitor1 "cat /monitor/bin/app/TradeMon/seatinfo.txt"'''
			#,,,,,,,,,,,,,,,,,,报单量,成交量,累计报单,累计成交,,连续交易峰值,,,,,,DownMsgNum,,
			#SEATINFO,10907,109,银河期货,1,10.118.136.113,front4,正确,1,0,1,online,SFTP V1.0,Lnx64 TraderAPI v1.5 L300,2015050807:50:56, ,2015050809:59:45,5,0,26,661,11914,1,32,2015050809:59:45,10,2,12,212751,178,28469,6119074
			#comd='''cat /tmp/seatinfo.txt'''
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read().decode('gbk').encode('utf8')
			#line=line.split('\n')
			
			thisfile=open("/tmp/seatinfo.txt","rb")
			line=thisfile.readlines()
			thisfile.close()
			
			seatinfo=[]
			onlinenumber=0
			offlinenumber=0
			nologinnumber=0
			
			seatinfo_define=[]
			onlinenumber_define=0
			offlinenumber_define=0
			nologinnumber_define=0
			
			seatinfo_femas=[]
			onlinenumber_femas=0
			offlinenumber_femas=0
			
			devicesum={}
			

			s=0
			for res in line:
				if "AllData" in res:
					continue
				try:
					res=res.decode('gbk').encode('utf8')
				except Exception:
					continue
				rest=res.split(',')

				#处理未登录的席位
				if len(rest)<16:
					try:
						defineflag=0
						try:
							defineflag=int(clientsdata[rest[1]][2])
						except:
							pass
						membername=rest[3]
						membernumber=rest[2]
						seatnumber=rest[1]
						yesterday_clients=0
						keyflag=1
						try:
							yesterday_clients=int(clientsdata[seatnumber][0])
							keyflag=int(clientsdata[seatnumber][1])
						except Exception:
							keyflag=0
							pass
						#seatstate=1:红,2:黄,3:绿,4:灰
						seatstate=4
						if keyflag==1:
							seatstate=1
						seatinfo.append({'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'status':'未登录','yesterday_clients':yesterday_clients,'seatstate':seatstate})
						if defineflag==1:
							nologinnumber_define=nologinnumber_define+1
							seatinfo_define.append({'membername':membername,'membernumber':membernumber,'seatnumber':seatnumber,'status':'未登录','yesterday_clients':yesterday_clients})
						continue
					except Exception:
						excinfo=traceback.format_exc()
						logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
						logfile.write(logdate+" \n"+excinfo+"\n")
						logfile.flush()
						continue

				#处理登录过的席位
				try:
					defineflag=0
					try:
						defineflag=int(clientsdata[rest[1]][2])
					except:
						pass
					membername=rest[3]
					membernumber=rest[2]
					seatnumber=rest[1]
					ip=rest[5]
					hostname=rest[6]
					limsg=rest[7]
					linum=rest[8]
					fainum=rest[9]
					status=rest[11]
					products=rest[12]
					version=rest[13]
					lintime=rest[14]
					if len(lintime)>=16:
						lintime= "%s-%s-%s %s" % (lintime[0:4],lintime[4:6],lintime[6:8],lintime[8:16])
					exittime=rest[15]
					if len(exittime)>=16:
						exittime= "%s-%s-%s %s" % (exittime[0:4],exittime[4:6],exittime[6:8],exittime[8:16])

					orderct=rest[20]
					tradect=rest[21]
					tradetop=rest[23]
					DownMsgNum=rest[29]
					yesterday_clients=0
					
					if len(seatnumber)<2:
						continue
					onlinenumber=onlinenumber+1
					keyflag=1
					try:
						yesterday_clients=int(clientsdata[seatnumber][0])
						keyflag=int(clientsdata[seatnumber][1])
					except Exception:
						keyflag=0
						pass
					#seatstate=1:红,2:黄,3:绿,4:灰
					seatstate=3
					if status=='offline':
						offlinenumber=offlinenumber+1
						if keyflag==1:
							seatstate=1
						else:
							seatstate=2
					
					#判断席位IP所属接入机房,接入路由器,总头,端口
					jifang=''
					devicename=''
					deviceport=''
					portdesc=''
					
					genres=gen_jifang(ip)
					jifang=genres['jifang']
					datacenter=genres['datacenter']
					devicename=genres['devicename']
					deviceport=genres['deviceport']
					portdesc=genres['portdesc']
					
					#处理设备统计信息,初始化与交易席位
					#devicesum_key=""
					#if devicename=="":
					#	devicesum_key=jifang+"-"+datacenter
					#else:
					#	devicesum_key=devicename+"-"+deviceport
					devicesum_key=jifang+"-"+datacenter+"-"+devicename+"-"+deviceport
					#print "----%s----" % devicesum_key
					if devicesum.has_key(devicesum_key):
						if status=='online':
							devicesum[devicesum_key]['tradeseat_online'] += 1
						elif status=='offline':
							devicesum[devicesum_key]['tradeseat_offline'] += 1
					elif devicesum_key != "":
						devicesum[devicesum_key]={
							'devicesum_key':devicesum_key,
							'devicename':devicename,
							'deviceport':deviceport,
							'jifang':jifang,
							'datacenter':datacenter,
							'tradeseat_online':0,
							'tradeseat_offline':0,
							'mdkseat_online':0,
							'mdkseat_offline':0
							}
						if status=='online':
							devicesum[devicesum_key]['tradeseat_online'] = 1
						elif status=='offline':
							devicesum[devicesum_key]['tradeseat_offline'] = 1
					
					seatdata={
						'membername':membername,
						'membernumber':membernumber,
						'seatnumber':seatnumber,
						'ip':ip,
						'hostname':hostname,
						'limsg':limsg,
						'linum':linum,
						'fainum':fainum,
						'status':status,
						'products':products,
						'version':version,
						'lintime':lintime,
						'exittime':exittime,
						'orderct':orderct,
						'tradect':tradect,
						'tradetop':tradetop,
						'DownMsgNum':DownMsgNum,
						'yesterday_clients':yesterday_clients,
						'seatstate':seatstate,
						'jifang':jifang,
						'datacenter':datacenter,
						'devicename':devicename,
						'deviceport':deviceport,
						'portdesc':portdesc
						}
					seatinfo.append(seatdata)
					if defineflag==1:
						onlinenumber_define=onlinenumber_define+1
						if status=='offline':
							offlinenumber_define=offlinenumber_define+1
						seatinfo_define.append(seatdata)
					if re.search('10.116.134|10.116.135|10.116.136|10.116.137|10.116.160|10.116.161|10.116.162|10.117.134|10.117.135|10.117.136|10.117.137|10.117.160|10.117.161',ip) or 'femas' in products.lower():
						onlinenumber_femas=onlinenumber_femas+1
						if status=='offline':
							offlinenumber_femas=offlinenumber_femas+1
						seatinfo_femas.append(seatdata)
						
				except Exception:
					excinfo=traceback.format_exc()
					logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
					logfile.write(logdate+" \n"+excinfo+"\n")
					logfile.flush()
					continue

			genjson('seatinfo',seatinfo)
			genjson_multi('seatinfo',seatinfo)
			genjson('seatinfo_define',seatinfo_define)
			genjson('seatinfo_femas',seatinfo_femas)
			#genjson_seat(seatinfo)
			
			strs="会员席位数：%s，在线席位数：%s，离线席位数：%s" % (onlinenumber,onlinenumber-offlinenumber,offlinenumber)
			file=open(lchome+"/pyweb/static/json/tradedata/seatinfototal.txt",'w')
			file.write(strs)
			file.close()

			strs="会员指定主席位数：%s，在线席位数：%s，离线席位数：%s，未登录席位数：%s" % (onlinenumber_define+nologinnumber_define,onlinenumber_define-offlinenumber_define,offlinenumber_define,nologinnumber_define)
			file=open(lchome+"/pyweb/static/json/tradedata/seatinfototal_define.txt",'w')
			file.write(strs)
			file.close()
			
			strs="会员席位数：%s，在线席位数：%s，离线席位数：%s" % (onlinenumber_femas,onlinenumber_femas-offlinenumber_femas,offlinenumber_femas)
			file=open(lchome+"/pyweb/static/json/tradedata/seatinfo_femastotal.txt",'w')
			file.write(strs)
			file.close()
			
			
			
			
			#行情席位统计
			#MDSEAT,303201,3032,3,10.116.133.2,mdkernel1,mdkernel,1,15182,[2]=566755616,正确,9,1,8,online,20150508071442,,上证所信息中心,,Win32 MdUserAPI v1.5 L300,
			#comd='''ssh ywn_monitor1 "cat /monitor/bin/app/TradeMon/mdkseat.txt"'''
			#comd='''cat /tmp/mdkseat.txt'''
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read()
			#line=line.decode('gbk').encode('utf8')
			#line=line.split('\n')
			
			thisfile=open("/tmp/mdkseat.txt","rb")
			line=thisfile.readlines()
			thisfile.close()
			
			mdkseat=[]
			onlinenumber=0
			offlinenumber=0
			
			for res in line:
				try:
					res=res.decode('gbk').encode('utf8')
				except Exception:
					continue
				rest=res.split(',')
				if len(rest)<20:
					continue
				try:
					seatnumber=rest[1]
					membernumber=rest[2]
					membername=rest[17]
					ip=rest[4]
					status=rest[14]
					hostname=rest[5]
					linum=rest[11]
					sucnum=rest[12]
					fainum=rest[13]
					limsg=rest[10]
					lintime=rest[15]
					if len(lintime)>=14:
						lintime="%s-%s-%s %s:%s:%s" % (lintime[0:4],lintime[4:6],lintime[6:8],lintime[8:10],lintime[10:12],lintime[12:14])
					exittime=rest[16]
					if len(exittime)>=14:
						exittime="%s-%s-%s %s:%s:%s" % (exittime[0:4],exittime[4:6],exittime[6:8],exittime[8:10],exittime[10:12],exittime[12:14])
					session=rest[9]
					mtype=rest[3]
					products=rest[18]
					version=rest[19]
					
					if len(seatnumber)<2:
						continue

					onlinenumber=onlinenumber+1
					if status=='offline':
						offlinenumber=offlinenumber+1
						
					#判断席位IP所属接入机房,接入路由器,总头,端口
					jifang=''
					devicename=''
					deviceport=''
					portdesc=''
					
					genres=gen_jifang(ip)
					jifang=genres['jifang']
					datacenter=genres['datacenter']
					devicename=genres['devicename']
					deviceport=genres['deviceport']
					portdesc=genres['portdesc']
					
					#处理设备统计信息,行情席位
					#devicesum_key=""
					#if devicename=="":
					#	devicesum_key=jifang+"-"+datacenter
					#else:
					#	devicesum_key=devicename+"-"+deviceport
					devicesum_key=jifang+"-"+datacenter+"-"+devicename+"-"+deviceport
					#print "----%s----" % devicesum_key
					if devicesum.has_key(devicesum_key):
						if status=='online':
							devicesum[devicesum_key]['mdkseat_online'] += 1
						elif status=='offline':
							devicesum[devicesum_key]['mdkseat_offline'] += 1
					elif devicesum_key != "":
						devicesum[devicesum_key]={
							'devicesum_key':devicesum_key,
							'devicename':devicename,
							'deviceport':deviceport,
							'jifang':jifang,
							'datacenter':datacenter,
							'tradeseat_online':0,
							'tradeseat_offline':0,
							'mdkseat_online':0,
							'mdkseat_offline':0
							}
						if status=='online':
							devicesum[devicesum_key]['mdkseat_online'] = 1
						elif status=='offline':
							devicesum[devicesum_key]['mdkseat_offline'] = 1
					
					mdkseatdata={
						"seatnumber":seatnumber,
						"membernumber":membernumber,
						"membername":membername,
						"ip":ip,
						"status":status,
						"hostname":hostname,
						"linum":linum,
						"sucnum":sucnum,
						"fainum":fainum,
						"limsg":limsg,
						"lintime":lintime,
						"exittime":exittime,
						"session":session,
						"mtype":mtype,
						"products":products,
						"version":version,
						"jifang":jifang,
						"datacenter":datacenter,
						"devicename":devicename,
						"deviceport":deviceport,
						"portdesc":portdesc
						}
					mdkseat.append(mdkseatdata)
				except IndexError:
					continue
				
			genjson('mdkseat',mdkseat)
			genjson_multi('mdkseat',mdkseat)
			#genjson_mdkseat(mdkseat)
			
			genjson_mdkseat_sum(mdkseat)
			
			strs="行情席位数：%s，在线席位数：%s，离线席位数：%s" % (onlinenumber,onlinenumber-offlinenumber,offlinenumber)
			file=open(lchome+"/pyweb/static/json/tradedata/mdkseattotal.txt",'w')
			file.write(strs)
			file.close()

			#行情席位相关告警
			mdk_schedule='0-4,9:15-11:30|0-4,12:45-15:00'
			time_state = lc_libs.lc_schedule(mdk_schedule)
			if time_state == True:
				temp_online=[]
				for line in mdkseat:
					#if line['membernumber'] not in temp_online and line['status']=='online':
					if line['membernumber'] not in temp_online and line['status']=='online' and int(line['membernumber'])<3000:
						temp_online.append(line['membernumber'])
				checknumber=int(members_numbers)-len(temp_online)
				print "members_numbers:%s,temp_online:%s,checknumber:%s" % (members_numbers,len(temp_online),checknumber)
				
				#超过6家会员的行情席位在所有的mdkernel上都没有登录:
				if checknumber>6:
					if alertflag['mdkseat_checknumber']>=2:
						alertflag['mdkseat_checknumber']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberMdk6OfflineErr'
						Type=1
						Severity=3
						Summary="有超过6家会员的行情席位在所有的mdkernel上都没有登录"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['mdkseat_checknumber'] += 1
				else:
					if alertflag['mdkseat_checknumber_r']>=2:
						alertflag['mdkseat_checknumber_r']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberMdk6OfflineErr'
						Type=2
						Severity=2
						Summary="超过6家会员的行情席位在所有的mdkernel上都没有登录事件恢复"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['mdkseat_checknumber_r'] += 1

				#超过15家会员的行情席位在所有的mdkernel上都没有登录:
				if checknumber>15:
					if alertflag['mdkseat_checknumber_over']>=2:
						alertflag['mdkseat_checknumber_over']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberMdk15OfflineCri'
						Type=1
						Severity=4
						Summary="有超过15家会员的行情席位在所有的mdkernel上都没有登录"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['mdkseat_checknumber_over'] += 1
				else:
					if alertflag['mdkseat_checknumber_over_r']>=2:
						alertflag['mdkseat_checknumber_over_r']=0
						AlertKey=time.strftime('%Y%m%d',time.localtime())
						AlertGroup='MemberMdk15OfflineCri'
						Type=2
						Severity=2
						Summary="超过15家会员的行情席位在所有的mdkernel上都没有登录事件恢复"
						strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
						print strs
						db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
					else:
						alertflag['mdkseat_checknumber_over_r'] += 1

			#设备席位统计
			genjson_devicesum('devicesum',devicesum)


			#会员链路状态统计
			#"up","安信期货"		      ,"0150"		   ,"交易结算会员"	,"电信" 	     ,"ATM"		  ,"6M" 	       ,"W_JR_JR_R03"	    ,"172.26.254.33"	 ,"10.112.14.42"      ,"10.116.138.0/24"   ,"ATM1/0/0.102-aal5 layer"		    ,"ATM0791"			   ,"直连外高桥机房"	,"北京"       ,"20140704"	  ,""			 ,""
			#"down","安信期货"		      ,"0150"		   ,"交易结算会员"	,"电信" 	     ,"SDH"		  ,"2M" 	       ,"W_JR_JR_R04"	    ,"172.26.254.34"	 ,"10.112.0.106"      ,"10.113.26.0/24"    ,"Serial1/1/0.1/2/4/1:0"		    ,"北京上海30N3998NP"	   ,"直连外高桥机房"	,"北京" 	     ,"20141222"	  ,""			 ,""
			#comd='''ssh ywn_monitor1 "cat /monitor/bin/app/TradeMon/mdkseat.txt"'''
			#comd='''cat /tmp/memberlink.csv'''
			#popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			#line=popen.stdout.read()
			#line=line.decode('gbk').encode('utf8')
			#line=line.replace(' ','').replace('\t','').replace('"','')
			#line=line.split('\n')
			
			thisfile=open("/tmp/memberlink.csv","rb")
			line=thisfile.readlines()
			thisfile.close()
			
			linestate=[]
			
			for res in line:
				try:
					res=res.decode('gbk').encode('utf8')
					res=res.replace(' ','').replace('\t','').replace('"','')
				except Exception:
					continue
				rest=res.split(',')
				if len(rest)<13:
					continue
				try:
					status=rest[0]
					membername=rest[1]
					membernumber=rest[2]
					membertype=rest[3]
					nettype=rest[4]
					linetype=rest[5]
					bandwidth=rest[6]
					devicename=rest[7]
					deviceip=rest[8]
					remoteip=rest[9]
					segment=rest[10]
					port=rest[11]
					lineno=rest[12]
					useage=rest[13]
					location=rest[14]
					cdate=rest[15]
					
					#if len(ip)<4 or len(membername)<2:
					#	continue
					
					linestate.append({"status":status,"membername":membername,"membernumber":membernumber,"membertype":membertype,"nettype":nettype,
						"linetype":linetype,"bandwidth":bandwidth,"devicename":devicename,"deviceip":deviceip,"remoteip":remoteip,"segment":segment,
							"port":port,"lineno":lineno,"useage":useage,"location":location,"cdate":cdate})
				except IndexError:
					continue
				
			genjson('linestate',linestate)
			genjson_multi('linestate',linestate)
			#genjson_linestate(linestate)
			
			#query_tradedata(tradesumtable,'^TF')
			#exit(0)
			time.sleep(10)

		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" \n"+excinfo+"\n")
			logfile.flush()
			time.sleep(10)
			continue

